package com.hitqz.robot.biz.config;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.hitqz.robot.api.business.dto.RemoteControlAuthDto;
import com.hitqz.robot.api.business.dto.RobotPatrolTaskDto;
import com.hitqz.robot.api.common.dto.mapArea.CalculateAreaMapDto;
import com.hitqz.robot.api.common.dto.robot.RobotPosInfoDto;
import com.hitqz.robot.api.common.util.CacheNameUtil;
import com.hitqz.robot.biz.core.FileProperties;
import com.hitqz.robot.driver.dto.robot.RobotPushDto;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

@Configuration
@Import({ FileProperties.class })
public class GuavaCacheConfig {

    //@Bean("robotCache")
    //public Cache<String, Object> robotCache() {
    //    return CacheBuilder.newBuilder()
    //            // 设置缓存的过期时间
    //            .expireAfterAccess(10, TimeUnit.SECONDS)
    //            // 设置缓存的最大容量
    //            .maximumSize(50)
    //            .build();
    //}

    /**
     * 遥控控制权
     */
    public final static String REMOTE_CONTROL_AUTH="remoteControlAuth";
    public final static String CACHE_CONST="Cache";
    @Bean(REMOTE_CONTROL_AUTH+CACHE_CONST)
    public Cache<String, Boolean> remoteControlAuthCache() {
        return CacheBuilder.newBuilder()
                // 设置缓存的过期时间
                .expireAfterWrite(5, TimeUnit.MINUTES)
                // 设置缓存的最大容量
                .maximumSize(1)
                .build();
    }

    @Bean("cmdResultCache")
    public Cache<String, Boolean> cmdCache() {
        return CacheBuilder.newBuilder()
                // 设置缓存的过期时间
                .expireAfterAccess(1, TimeUnit.SECONDS)
                // 设置缓存的最大容量
                .maximumSize(25)
                .build();
    }

    @Bean("lockCache")
    public Cache<String, Lock> lockCache() {
        return CacheBuilder.newBuilder()
                // 设置缓存的最大容量
                .maximumSize(50)
                .build();
    }

    @Bean("conditionCache")
    public Cache<String, Condition> conditionCache() {
        return  CacheBuilder.newBuilder()
                // 设置缓存的最大容量
                .maximumSize(50)
                .build();
    }

    @Bean("flowCache")
    public Cache<String,Object> flowCache(){
        return CacheBuilder.newBuilder()
                .expireAfterWrite(4, TimeUnit.SECONDS)
                // 设置缓存的最大容量
                .maximumSize(10)
                .build();
    }

    @Bean(CacheNameUtil.CACHE_ROBOT_INFO)
    public Cache<String, RobotPushDto> webPosCache(){
        return CacheBuilder.newBuilder()
                .expireAfterWrite(10, TimeUnit.SECONDS)
                .maximumSize(10)
                .build();
    }

    @Bean(CacheNameUtil.CACHE_MAP_AREA)
    public Cache<Long, CalculateAreaMapDto> mapArea(){
        return CacheBuilder.newBuilder()
                .maximumSize(30)
                .build();
    }


    @Bean(CacheNameUtil.CACHE_POS_INFO)
    public Cache<String, RobotPosInfoDto> posInfo(){
        return CacheBuilder.newBuilder()
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .maximumSize(10)
                .build();
    }


    /**
     * 地图建模数据
     * @return
     */
    @Bean(CacheNameUtil.MAP_MODELING)
    public Cache<String, int[][]> mapModeling(){
        return CacheBuilder.newBuilder()
                .maximumSize(10)
                .build();
    }

    @Bean(CacheNameUtil.MAP_AREA_ACTION_RESULT)
    public Cache<String, Map<Integer, Boolean>> robotStatusCache(){
        return CacheBuilder.newBuilder()
                .maximumSize(10)
                .build();
    }


    @Bean("nextStep")
    public Cache<String, Boolean> nextStep() {
        return CacheBuilder.newBuilder()
                // 设置缓存的过期时间
                //.expireAfterAccess(1, TimeUnit.SECONDS)
                // 设置缓存的最大容量
                .maximumSize(25)
                .build();
    }

    /**
     * 视频流输出是否存活
     * @return
     */
    @Bean("videoStreamOutputAlive")
    public Cache<String, Boolean> videoStreamOutputAlive() {
        return CacheBuilder.newBuilder()
                // 设置缓存的过期时间
                .expireAfterWrite(5, TimeUnit.SECONDS)
                // 设置缓存的最大容量
                .maximumSize(50)
                .build();
    }

    @Bean("robotWebRefreshCache")
    public Cache<String, Boolean> robotWebRefreshCache() {
        return CacheBuilder.newBuilder()
                // 缓存时间
                .expireAfterWrite(4, TimeUnit.SECONDS)
                // 缓存数量
                .maximumSize(1)
                .build();
    }


}
